package com.sofwin.configer;

import com.sofwin.service.UserService;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @packageName: com.sofwin.configer
 * @author: andyliu
 * @date: 2022/5/7 10:08
 * @email 115176513@qq.com
 * @description: TODO
 */
@Configuration
public class ShiroConfiger{

   @Bean
   public HashedCredentialsMatcher hashedCredentialsMatcher(){
       HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
       matcher.setHashAlgorithmName("md5");// 加密规则
       matcher.setHashIterations(1);// 设置hash次数
       matcher.setStoredCredentialsHexEncoded(true);
       return matcher;
   }
   @Bean
   public MyRealm myRealm(){
       MyRealm  myRealm= new MyRealm();
       myRealm.setCredentialsMatcher(hashedCredentialsMatcher());
       return myRealm;
   }

    @Bean
    public DefaultWebSecurityManager getSecurityManager(){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(myRealm());

        return manager;
    }

    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        // 设置过滤器使用的安全管理器
        bean.setSecurityManager(getSecurityManager());
        // 设置登录的地址
        bean.setLoginUrl("/login.jsp");

        bean.setFilterChainDefinitions("/index.html = anon\n" +
                "            /login = anon\n" +
                "            /css/** = anon\n" +
                "            /**/*.css = anon\n" +
                "            /**/*.js = anon\n" +
                "            /img/** = anon\n" +
                "            /kaptcha = anon\n" +
                "            /login.jsp = anon\n" +
                "           /** = authc");
        return bean;
    }

}
